<?php
include("function.php");
$renum=$_POST['renum'];
$num=20;//要返回的数据量
$minlim=($renum-1)*$num;
$maxlim=$renum*$num;
if($_POST['request_type']==1){

if($_POST['way']==0)
	{//最新
		if($_POST['clsfy']!=0)
		{	//分类
			$sql="select * from `image` where `clsfy_id`='".$_POST['clsfy']."' and `del`='".$_POST['over']."' order by create_time desc limit ".$minlim.",".$num."";
		}else{
			$sql="select * from `image` where `del`='".$_POST['over']."' order by create_time desc limit ".$minlim.",".$num."";}
		}
	}else
	{//最热
		if($_POST['clsfy']!=0)
		{	//分类
			$sql="select * from `image` where `clsfy_id`='".$_POST['clsfy']."' and `del`='".$_POST['over']."' order by concern desc limit ".$minlim.",".$num."";
		}else{
			$sql="select * from `image` where `del`='".$_POST['over']."' order by concern desc limit ".$minlim.",".$num."";}
	}

if($_POST['request_type']==2){$sql="select * from `image` where `del`='0' order by concern desc limit ".$minlim.",".$num."";}
if($_POST['request_type']==3){
	if($_POST['gallery'] != ""){
		$sql="select * from `image` where gallery='$_POST[gallery]' and `del`='0' order by create_time desc limit ".$minlim.",".$num;
	}else{
		$sql="select * from `image` where id in(select img_id from image_like where `user`='$_POST[like_user]') order by create_time desc limit ".$minlim.",".$num;
	}
}
if($_POST['request_type']==4){$sql="select * from `image` where `del`='0' and `name` like '%$_POST[searchs]%' or address like '%$_POST[searchs]%' or gallery in(select `id` from gallery where `name` like '%$_POST[searchs]%') order by concern desc limit ".$minlim.",".$num;}
$re=mysql_query($sql)or die(mysql_error());
$data = array();
//echo $sql;
while($row=mysql_fetch_array($re)){
	$i=0;$k=0;$owner=0;$update='';
	if($_SESSION['login_mail'] == $row['post_user'])$owner=1;
	for($s=0;$s<3;$s++){$u[$s]="";$comm[$s]="";$n[$s]="";$t[$s]="";}
	$result=mysql_query("select * from `user` where user_email='$row[post_user]'");
	$r=mysql_fetch_array($result);
	$p_uid=$r['id'];//用户ID
	$p_uname=$r['user_name'];//用户名
	//图片更新
	$result=mysql_query("select * from `image_update` where `img_id`='$row[id]'");
	while($r=mysql_fetch_array($result)){
		$update[]=$r['id'];
	}
	//是否已喜欢
	$result=mysql_query("select * from `image_like` where user='$_SESSION[login_mail]' and `img_id`='$row[id]'");
	$r=mysql_num_rows($result);
	$like=$r;
	$result=mysql_query("select * from comment where img_id='$row[id]' order by create_time desc");
	while($r=mysql_fetch_array($result)){
		if($k==3){break;}//返回三条评论
		$rs=mysql_query("select * from `user` where `user_email`='$r[comm_user]'");
		$rows=mysql_fetch_array($rs);
		$n[$i]=$rows['user_name'];//comm_name
		$u[$i]=$rows['id'];//comm_user_id
		$comm_user[$i]=$r['comm_user'];
		$comm[$i]=$r['comment'];//comment
		$t[$i]=$r['create_time'];//comment_time
		$k++;$i++;
		}
		//var_dump($u);
	//c:评论 u:用户ID t:评论时间 n:用户名 uid:上传图片的用户ID lng:经度 lat:纬度 puname:上传图片的用户名
	$data[] = (object)array("image"=>"image.php?id=".$row['id']."&type=media","title" => $row['name'],"id" => $row['id'],"address" => $row['address'],"uid" => $p_uid,"puname" => $p_uname,"cuid" => $u,"comm" => $comm,"cuname" => $n,"comm_time" => $t,"cu_mail" => $comm_user,"owner" => $owner,"like" => $like,"update" => $update,"like_num" => $row['concern'],"finished" => $row['finished']);
	}

echo json_encode( $data );
//echo $sql;
//var_dump($json);
?>